<?php

namespace App\Jobs\Auth;

use App\Jobs\Helper as JobHelper;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Hash;

class Login implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, JobHelper;

    protected $request;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $account = User::account($this->request->username)->first();
        if (!$account) {
            self::throwException('用户不存在');
        }
//        if ($account->status == Manager::STATUS_OFF) {
//            self::throwException('此账号已被禁用');
//        }
//        if ($account->primary == 0) {
//            self::throwException('非项目经理不能登录此系统');
//        }

        if (Hash::check($this->request->password, $account->password)) {
            return $account;
        }
        self::throwException('密码错误');
    }

}
